<template>
  <a-modal
    title="创建选题"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    class="topic_modal"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选题名称">
          <a-input placeholder="请输入选题名称" v-decorator="['topicName', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选题描述">
          <a-input type="textarea" :row="6" placeholder="请输入选题描述" v-decorator="['topicDesc', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="媒体类型">
          <a-select placeholder="请选择选题类型" v-decorator="['topicType', {}]">
            <a-select-option value="1">传统媒体</a-select-option>
            <a-select-option value="2">新媒体</a-select-option>
            <a-select-option value="3">传统媒体+新媒体（全媒体）</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input type="textarea" :row="4" placeholder="请输入备注" v-decorator="['remark', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {httpAction, postAction} from "../../../api/manage";

  export default {
    name: "TasTopicInfoModal",
    data() {
      return {
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "task/innerTopic/createInnerTopic",
          edit: "task/innerTopic/updateInnerTopic",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        if (record && record.topicType) {
          record.topicType = record.topicType + ""
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'remark', 'topicName', 'topicDesc', 'topicType'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            this.confirmLoading = true;
            if (this.model.id) {
              this.editTopic(Object.assign(this.model, values));
            } else {
              this.addTopic(Object.assign(this.model, values))
            }
          }
        })
      },
      addTopic(formData) {
        getAction(this.url.add, formData).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('ok');
          } else {
            this.$message.warning(res.message);
          }
        }).finally(() => {
          this.confirmLoading = false;
          this.close();
        })
      },
      editTopic(formData) {
        postAction(this.url.edit, formData).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('ok');
          } else {
            this.$message.warning(res.message);
          }
        }).finally(() => {
          this.confirmLoading = false;
          this.close();
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style lang="less" scoped>
  .topic_modal {
    z-index: 9999;
  }

  .topic_modal .ant-modal-wrap {
    z-index: 999999 !important;
  }
</style>